﻿// -----------------------------------------------------------------------
// <copyright file="GalleryController.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace Epam.PhotoGallery.Site.Controllers
{
	using System;
	using System.Configuration;
	using System.IO;
	using System.Linq;
	using System.Web.Mvc;
	using Epam.PhotoGallery.Bussines.Configuration;
	using Epam.PhotoGallery.Bussines.Providers;
	using Epam.PhotoGallery.Interfaces.DataTransferObjects;
	using Epam.PhotoGallery.Interfaces.Providers;
	using Epam.PhotoGallery.Site.Models;

	/// <summary>
	/// Home controller
	/// </summary>
	[HandleError]
	public class GalleryController : Controller
	{
		#region Fields

		/// <summary>
		/// ITimelineDataProvider provider
		/// </summary>
		private ITimelineDataProvider provider;

		#endregion

		#region Properties

		/// <summary>
		/// Gets the data provider.
		/// </summary>
		public ITimelineDataProvider DataProvider
		{
			get
			{
				if (this.provider == null)
				{
					this.provider = new FileSystemTimelineDataProvider();
				}
				return this.provider;
			}
		}

		/// <summary>
		/// Gets or sets the model.
		/// </summary>
		/// <value>
		/// The model.
		/// </value>
		public TimeLine Model { get; set; }

		#endregion

		/// <summary>
		/// Indexes this instance.
		/// </summary>
		/// <returns>
		/// Return index view.
		/// </returns>
		
		public ViewResult Index(bool isUpdate)
		{
			

			RepositorySection repository = (RepositorySection)ConfigurationManager.GetSection(RepositorySection.RepositorySectionName);

			var path = repository.IsRelative ? Path.Combine(Server.MapPath("~"), repository.Path) : repository.Path;
			
			if (isUpdate)
			{
				TimeLine.UpdateModel(this.DataProvider, path);
			}

			var result = TimeLine.GetModel(this.DataProvider, path);
			return View(result);
		}


		/// <summary>
		/// Gets the data for albums.
		/// </summary>
		/// <param name="albumId">The album id.</param>
		/// <returns>
		/// Return Data for Albums.
		/// </returns>
		public JsonResult GetDataForAlbums(string albumId)
		{
			var items = Enumerable.Empty<GalleryImage>();

			Guid currentId;
			if (Guid.TryParse(albumId, out currentId))
			{
				RepositorySection repository = (RepositorySection)ConfigurationManager.GetSection(RepositorySection.RepositorySectionName);

				var path = repository.IsRelative ? Path.Combine(Server.MapPath("~"), repository.Path) : repository.Path;

				var model = TimeLine.GetModel(this.DataProvider, path);

				var albumPath = model.GetAlbumPathById(currentId);

				items = this.DataProvider.GetImagesForAlbum(albumPath);

				model.Events.First(t => t.Id.Equals(currentId)).Images = items;
			}

			var result = new JsonResult();

			result.Data = items;
			result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

			return result;
		}

		/// <summary>
		/// Gets the image for Album.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <param name="w">The w.</param>
		/// <param name="h">The h.</param>
		/// <returns>
		/// Return picture result flow.
		/// </returns>
		public PictureResult GetImage(string id, int w, int h)
		{
			Guid currentId;
			if (Guid.TryParse(id, out currentId))
			{
				RepositorySection repository = (RepositorySection)ConfigurationManager.GetSection(RepositorySection.RepositorySectionName);

				var path = repository.IsRelative ? Path.Combine(Server.MapPath("~"), repository.Path) : repository.Path;

				var model = TimeLine.GetModel(this.DataProvider, path);
				var imagePath = model.GetImagePathById(currentId);
				return new PictureResult(imagePath, w, h);
			}

			return null;
		}

		/// <summary>
		/// Gets the cover.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <param name="w">The w.</param>
		/// <param name="h">The h.</param>
		/// <returns>
		/// Return picture result flow.
		/// </returns>
		public PictureResult GetCover(string id, int w, int h)
		{
			Guid currentId;
			if (Guid.TryParse(id, out currentId))
			{
				RepositorySection repository = (RepositorySection)ConfigurationManager.GetSection(RepositorySection.RepositorySectionName);

				var path = repository.IsRelative ? Path.Combine(Server.MapPath("~"), repository.Path) : repository.Path;

				var model = TimeLine.GetModel(this.DataProvider, path);
				var imagePath = model.GetAlbumCoverPathById(currentId);
				return new PictureResult(imagePath, w, h);
			}

			return null;
		}

		public PictureResult GetControlImage(string id, int w, int h)
		{
			Guid currentId;
			if (Guid.TryParse(id, out currentId))
			{
				RepositorySection repository = (RepositorySection)ConfigurationManager.GetSection(RepositorySection.RepositorySectionName);

				var path = repository.IsRelative ? Path.Combine(Server.MapPath("~"), repository.Path) : repository.Path;

				var model = TimeLine.GetModel(this.DataProvider, path);
				var imagePath = model.GetAlbumCoverPathById(currentId);
				return new PictureResult(imagePath, w, h);
			}

			return null;
		}
	}
}